/*
* @param
* @author: Xiaoming Wang
* type: int|number|date|string
* nullable:true|false
* regex:^[-\+]?\d+$
* min:0
* max:999999999
* campare:id
* comparetype:eq,neq,leq,req,le,ri
* tipcontrol:
* emptyTip:
*/
var ErrorMessage; 
function jquery_ui_formvalidate(form) 
{
	ErrorMessage=''; 
	var legality,items; 
	legality = true; 
	items = $(form).find("input[validator]"); 
	for(var i =0;i< items.length;i++) 
	{ 
		if(legality) 
		{ 
			legality = OnItemValidator($(items[i])).result; 
		}
		else 
		{
			OnItemValidator($(items[i])).result; 
		}
	}
	items = $(form).find("textarea[validator]"); 
	for(var i =0;i< items.length;i++) 
	{ 
		if(legality) 
		{ 
			legality = OnItemValidator($(items[i])).result; 
		} 
		else 
		{ 
			OnItemValidator($(items[i])).result; 
		} 
	}
	items = $(form).find("select[validator]"); 
	for(var i =0;i< items.length;i++) 
	{ 
		if(legality) 
		{ 
			legality = OnItemValidator($(items[i])).result; 
		} 
		else 
		{ 
			OnItemValidator($(items[i])).result; 
		} 
	}
	if(!legality) 
	{
		if(ErrorMessage !='') alert(ErrorMessage); 
	}
	return legality; 
}

function CreateValObject(validator) 
{ 
	var valobj = {
		type: 'string', 
		nullable: true,
		regex: null,
		min: null,
		max: null,
		campare: null,
		comparetype: null,
		tipcontrol: null,
		emptyTip: null,
		maxLength: null,
		minLength: null,
		readOnly: false
	};
	var properties;
	var execute;
	var namevalue;
	
	properties = validator.split(';');
	for(i=0;i<properties.length;i++) 
	{
		if(properties[i]!=""){
			namevalue = properties[i].split(':'); 
			execute ="valobj." + namevalue[0] +'=\''+ namevalue[1]+'\';'; 
			eval(execute);
		}
	}
	return valobj; 
}

function OnItemValidator(control) 
{ 
	var regex, maxvalue, minvalue, cvalue; 
	var valobj = CreateValObject(control.attr('validator')); 
	var value = control.val();
	var resultObj = {result: true, resultMsg: null};
	
	value = ValidatorConvert(value, valobj.type); 
	if(valobj.nullable == "false")
	{
		if(value == null || value=="") 
		{
			var _input_Id = control.attr('id');
			var _emptyTip = valobj.emptyTip;
			var alt_msg = "";
			if(_emptyTip == null || _emptyTip == "")
			{
				alt_msg = "This field is required";
			}
			else
			{
				alt_msg = _emptyTip;
			}
			resultObj.resultMsg = alt_msg;
			
			resultObj.result = false;
			return resultObj;
		}
	}
	else
	{
		if(value == null || value== "")
		return resultObj;
	}
	if(valobj.regex != null)
	{
		regex = new RegExp(valobj.regex);
		if(value.match(regex) == null)
		{
			ValidatorError(valobj);
			
			resultObj.result = false;
			return resultObj;
		}
	}
	if(valobj.min != null)
	{
		minvalue = ValidatorConvert(valobj.min,valobj.type); 
		if(!CompareValue(value,minvalue,"req")) 
		{
			var _input_Id = control.attr('id');
			var alt_msg = "The min value here is " + valobj.min;
			resultObj.resultMsg = alt_msg;
			//ValidatorError(valobj);
			resultObj.result = false;
			return resultObj;
		}
	}
	if(valobj.max != null)
	{
		maxvalue = ValidatorConvert(valobj.max,valobj.type); 
		if(!CompareValue(value,maxvalue,"leq")) 
		{
			var _input_Id = control.attr('id');
			var alt_msg = "The max value here is " + valobj.max;
			resultObj.resultMsg = alt_msg;
			ValidatorError(valobj);
			resultObj.result = false;
			return resultObj;
		}
	} 
	if(valobj.campare != null)
	{
		cvalue = $('#' + valobj.campare).val();
		cvalue = ValidatorConvert(cvalue,valobj.type);
		if(!CompareValue(value,cvalue,valobj.comparetype))
		{
			ValidatorError(valobj);
			resultObj.result = false;
			return resultObj;
		}
	}
	if(valobj.minLength != null)
	{
		var _input_Id = control.attr('id');
		var _input_Value = control.val();
		var _input_length = _input_Value.length;
		if(_input_length < valobj.minLength)
		{
			ValidatorError(valobj);
			var alt_msg = "The min length is " + valobj.minLength;
			resultObj.resultMsg = alt_msg;
			resultObj.result = false;
			return resultObj;
		}
	}
	if(valobj.maxLength != null)
	{
		var _input_Id = control.attr('id');
		var _input_Value = control.val();
		var _input_length = _input_Value.length;
		if(_input_length > valobj.maxLength)
		{
			ValidatorError(valobj);
			var alt_msg = "The max length is " + valobj.maxLength;
			resultObj.resultMsg = alt_msg;
			resultObj.result = false;
			return resultObj;
		}
	}
	
	return resultObj;
}

function ValidatorError(valobj) 
{
	if(valobj.tipcontrol != null) showTip($("#"+ valobj.tipcontrol)); 
	else
	{
		if(ErrorMessage !='') ErrorMessage += '\n'; 
		ErrorMessage += valobj.emptyTip; 
	}
}

function CompareValue(leftvalue,rightvalue,compareType) 
{ 
	if(leftvalue == null || rightvalue == null) 
	return false; 
	if(compareType=="eq") 
	{ 
		return leftvalue == rightvalue; 
	} 
	else if(compareType =="neq") 
	{ 
		return leftvalue != rightvalue; 
	} 
	else if(compareType =="le") 
	{ 
		return leftvalue < rightvalue; 
	} 
	else if(compareType =="leq") 
	{ 
		return leftvalue <= rightvalue; 
	} 
	else if(compareType =="ri") 
	{ 
		return leftvalue > rightvalue; 
	} 
	else if(compareType =="req") 
	{ 
		return leftvalue >= rightvalue; 
	} 
	else 
	{ 
		return false; 
	}
}
function showTip(control) 
{
	if(control.attr('show') != 'on') 
	{
		control.fadeIn("slow"); 
		control.attr('show','on'); 
	}
}
function hideTip(control) 
{
	control.hide(); 
	control.attr('show',''); 
}

function ValidatorConvert(value, dataType) {
	var num, exp, m; 
	var year, month, day 
	if(value == null || value == "") return null; 
	if(dataType=="int") 
	{
		exp=/^[-\+]?\d+$/;
		if (value.match(exp) == null)
		return null;
		num = parseInt(value, 10);
		return (isNaN(num) ? null : num);
	}
	else if(dataType =="number")
	{
		exp=/^[-\+]?((\d+)|(\d+\.\d+))$/;
		if (value.match(exp) == null)
		return null;
		num = parseFloat(value);
		return (isNaN(num) ? null : num);
	}
	else if(dataType =="date")
	{
		exp=/^(\d{4})([-/]?)(\d{1,2})([-/]?)(\d{1,2})$/
		m = value.match(exp);
		if (m == null)
		{
			exp=/^(\d{1,2})([-/]?)(\d{1,2})([-/]?)(\d{4})$/
			m = value.match(exp);
			if(m== null)
			return null;
			year = m[5];
			month = m[1];
			day =m[3];
		}
		else 
		{
			year = m[1];
			month =m[3];
			day = m[5];
		}
		try 
		{ 
			num = new Date(year,month,day); 
		} 
		catch(e) 
		{ 
			return null; 
		} 
		return num; 
	}
	else{ 
		return value.toString(); 
	}
}
